package org.example.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.listener.MyMessageListener1;
import org.example.listener.MyMessageListener2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory connectionFactory) {
        //创建一个RedisTemplate实例，并指定key为String类型，value为Long类型。
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        //设置RedisTemplate的连接工厂，用于与Redis服务器建立连接。
        template.setConnectionFactory(connectionFactory);
        //设置RedisTemplate的key序列化器，用于将Java对象序列化成Redis中的键（key），
        //这里使用了Spring提供的StringRedisSerializer来进行序列化。
        template.setKeySerializer(new StringRedisSerializer());
        //设置RedisTemplate的value序列化器，用于将Java对象序列化成Redis中的值（value），
        //这里使用了Spring提供的GenericToStringSerializer来进行序列化，
        //将Long类型的数据转换为字符串存储到Redis中。
//        template.setValueSerializer(new GenericToStringSerializer<>(Long.class));

        template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        return template;
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate2(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        // key
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        // value
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    private final RedisConnectionFactory redisConnectionFactory;

    public RedisConfig(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Bean(name="stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean(name="stringRedisTemplateTransaction")
    public StringRedisTemplate stringRedisTemplateTransaction() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setEnableTransactionSupport(true);
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

//    @Autowired
//    private MyMessageListener1 myMessageListener1;
//    @Autowired
//    private MyMessageListener2 myMessageListener2;
//    @Bean
//    public RedisMessageListenerContainer redisContainer() {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        container.setConnectionFactory(redisConnectionFactory);
//        container.addMessageListener(myMessageListener1, new ChannelTopic("my_channel1"));
//        container.addMessageListener(myMessageListener2, new ChannelTopic("my_channel2"));
//        return container;
//    }

}
